﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Webbshop.ServiceReference1;
using System.Drawing;

namespace Webbshop
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // Only once
            if (!Page.IsPostBack)
            {
                UpdateCultureCombobox();
                fillMenu(LoginView1.FindControl("NavigationMenu") as Menu);
                Product currentProduct = Application["Products"] as Product;
                

                randomProd.ImageUrl = "~/Content/" + currentProduct.ImageURL;
                randomProd.PostBackUrl = "~/ProductDetail.aspx?productID=" + currentProduct.ID;
            }

            // Allways
            SetShoppingCart();
        }

        /// <summary>
        /// Hämtar/Skapar Varukorg och fyller i antalet Produkter i menyn
        /// </summary>
        private void SetShoppingCart()
        {
            Session["ShoppingCart"] = Functions.GetCart(Session["ShoppingCart"] as Cart);
            var userMenu = loginUserMenu.FindControl("UserMenu") as Menu;

            int productsInCart = 0;
            foreach (var item in (Session["ShoppingCart"] as Cart).Products)
            {
                productsInCart += item.Value;
            }

            HttpCookie cultureCookie = Request.Cookies["Culture"];
            string cultureCode = (cultureCookie != null) ? cultureCookie.Value : null;
            if (string.IsNullOrEmpty(cultureCode))
                Response.Cookies["Culture"].Value = "sv-SE";

            if (productsInCart == 1)
            {
                // Kontrollera vilket språk
                if (Request.Cookies["Culture"].Value == "sv-SE")
                    userMenu.Items[0].Text = "Kundvagnen - Du har just nu: " + productsInCart.ToString() + " vara.";
                else
                    userMenu.Items[0].Text = "ShoppingCart - You have: " + productsInCart.ToString() + " product.";
            }
            else
                {
                    // Kontrollera vilket språk
                if (Request.Cookies["Culture"].Value == "sv-SE")
                    userMenu.Items[0].Text = "Kundvagnen - Du har just nu: " + productsInCart.ToString() + " varor.";
                else
                    userMenu.Items[0].Text = "ShoppingCart - You have: " + productsInCart.ToString() + " products.";
                }
            
        }

        /// <summary>
        /// Fill targetMenu with text och link to all categorys 
        /// </summary>
        /// <param name="targetMenu">The menu you want to fill</param>
        private void fillMenu(Menu targetMenu) 
        {
            ShoppeServiceClient proxy = new ShoppeServiceClient();
            
            foreach (String category in proxy.getAllCategorys())
            {
                targetMenu.Items.Add(
                    new MenuItem(){
                        Text = category,
                        NavigateUrl = "~/overViewPage.aspx?category=" + category
                    }
                );
            }
            proxy.Close();
        }

        /// <summary>
        /// Click Event that redirect the user to the search result container, 
        /// including the JQuery value from the searchBox.Text 
        /// </summary>
        protected void SearchButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/SearchResultPage.aspx?q=" + SearchBox.Text);
        }

        /// <summary>
        /// Event när användare ändrar i Combobox för språk/kultur.
        /// Skapar en cookie för valt språk/kultur och hämtar sidan igen
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void cmbCulture_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Cookies.Add(new HttpCookie("Culture", cmbCulture.SelectedValue));

            Response.Redirect(Request.Url.AbsolutePath + Request.Url.Query);
        }

        /// <summary>
        /// Markerar den rad i Comboboxen som motsvarar aktuellt språk
        /// </summary>
        protected void UpdateCultureCombobox()
        {
            HttpCookie cultureCookie = Request.Cookies["Culture"];
            string cultureCode = (cultureCookie != null) ? cultureCookie.Value : null;
            if (!string.IsNullOrEmpty(cultureCode))
            {
               cmbCulture.SelectedValue = cultureCode;
            }
        }
    }
}
